﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.OpenAuth.Domain.Core;
using Zj.OpenAuth.Domain.Interface;
using Zj.OpenAuth.DTO.Common;

namespace Zj.OpenAuth.App.DepartmentApp
{
    /// <summary>
    /// department application service  implement
    /// </summary>
    public class DepartmentAppService : IDepartmentAppService
    {
        public readonly IDepartmentRepository _repository;
        public DepartmentAppService(IDepartmentRepository repository)
        {
            this._repository = repository;
        }


        /// <summary>
        /// add or edit department 
        /// </summary>
        /// <returns></returns>
        public bool AddOrEdit(DepartmentDTO dto)
        {
            var department = _repository.InsertOrUpdate(Mapper.Map<DepartmentDTO, Department>(dto));
            return department == null ? false : true;
        }



        /// <summary>
        /// delete department
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void Delete(int id)
        {
            _repository.Delete(id);
        }


        public void DeleteMuti(List<int> ids)
        {
            ids.ForEach(id => _repository.Delete(id));
        }

        public DepartmentDTO Get(int id)
        {
            return Mapper.Map<Department, DepartmentDTO>(_repository.Get(id));
        }


        public List<DepartmentDTO> GetAllList()
        {
            return Mapper.Map<List<Department>, List<DepartmentDTO>>(_repository.Query(s => s.Status != StatusEnum.Deleted).ToList());
        }

        public List<DepartmentDTO> GetChildrenByParent(int? id, int pageIndex, out int rowCount, int pageSize = 10)
        {
            return Mapper.Map<List<Department>, List<DepartmentDTO>>(
                _repository.QueryPage(s => s.Status != StatusEnum.Deleted && s.ParentId == id.Value, pageSize, pageIndex, out rowCount, s => s.Id)
                .ToList());
        }
    }
}
